//
//  RegisterViewController.m
//  QuickCure1
//
//  Created by maoge on 16/10/30.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "RegisterViewController.h"
#import "LoginViewController.h"
#import "LoginTextField.h"
#import <SMS_SDK/SMSSDK.h>

@interface RegisterViewController ()

@property(nonatomic,weak)UIButton *getCodeBtn;

@property(nonatomic,weak)UITextField *userName_tf;
@property(nonatomic,weak)UITextField *passWord_tf;
@property(nonatomic,weak)UITextField *invite_tf;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self setupUI];
    
}


- (void)setupUI
{
    self.title = @"新用户注册";
    
    LoginTextField *userName_tf = [[LoginTextField alloc]initWithPlaceholder:@"请输入您的手机号"];
    LoginTextField *passWord_tf = [[LoginTextField alloc]initWithPlaceholder:@"请输入您的验证码"];
    LoginTextField *invite_tf = [[LoginTextField alloc]initWithPlaceholder:@"请输入您的邀请码(选填)"];
    
    _userName_tf = userName_tf;
    _passWord_tf = passWord_tf;
    _invite_tf = invite_tf;
    
    //数字键盘
    userName_tf.keyboardType = UIKeyboardTypeNumberPad;
    passWord_tf.keyboardType = UIKeyboardTypeNumberPad;
    
    
    [self.view addSubview:userName_tf];
    [self.view addSubview:passWord_tf];
    [self.view addSubview:invite_tf];
    
    //获取短信验证码按钮
    UIButton *getCodeBtn = [[UIButton alloc]init];
   // _getCodeBtn = getCodeBtn;
    [getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    getCodeBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [getCodeBtn setTitleColor:kThemeColor forState:UIControlStateNormal];
    [getCodeBtn setBackgroundColor:[UIColor colorWithWhite:0.888 alpha:1]];
    
    [self.view addSubview:getCodeBtn];
    
    //添加获取短信验证码监听
    [getCodeBtn addTarget:self action:@selector(getCode) forControlEvents:UIControlEventTouchUpInside];
    
    
    //本人已同意并阅读用户协议
    UILabel *agree_lbl = [[UILabel alloc]init];
    agree_lbl.text = @"本人已同意并阅读";
    [self.view addSubview:agree_lbl];
    
    UIButton *user_btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [user_btn setTitle:@"用户协议" forState:UIControlStateNormal];
    [self.view addSubview:user_btn];
    
    //下一步
    UIButton *next_btn = [[UIButton alloc]init];
    [next_btn setTitle:@"下一步" forState:UIControlStateNormal];
    next_btn.backgroundColor = kThemeColor;
    [self.view addSubview:next_btn];
    
    //直接登录
    UIButton *direct_btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [direct_btn setTitle:@"已有注册用户,重新登录" forState:UIControlStateNormal];
    [self.view addSubview:direct_btn];
    
    
    
    [userName_tf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view).offset(20);
        make.trailing.equalTo(self.view).offset(-20);
        make.top.equalTo(self.view).offset(130);
    }];
    
    [passWord_tf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view).offset(20);
        make.trailing.equalTo(self.view).offset(-150);
        make.top.equalTo(userName_tf.mas_bottom).offset(30);
    }];
    
    [getCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(passWord_tf.mas_trailing).offset(30);
        make.top.equalTo(userName_tf.mas_bottom).offset(30);
    }];
    
    [invite_tf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view).offset(20);
        make.trailing.equalTo(self.view).offset(-20);
        make.top.equalTo(passWord_tf.mas_bottom).offset(30);
    }];
    
    [agree_lbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view).offset(70);
        make.top.equalTo(invite_tf.mas_bottom).offset(50);
    }];
    [user_btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(agree_lbl.mas_trailing).offset(10);
        make.centerY.equalTo(agree_lbl);
    }];
    
    [next_btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view).offset(20);
        make.trailing.equalTo(self.view).offset(-20);
        make.top.equalTo(agree_lbl.mas_bottom).offset(30);
    }];
    
    [direct_btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(next_btn.mas_bottom).offset(100);
    }];
    
    
    //监听方法
    [direct_btn addTarget:self action:@selector(directLogin) forControlEvents:UIControlEventTouchUpInside];
    
}
#pragma mark -
#pragma mark - 直接登录
- (void)directLogin
{
    NSLog(@"%zd",self.navigationController.viewControllers.count);
    
    if (self.navigationController.viewControllers.count == 3) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
//        LoginViewController *loginVc = [[LoginViewController alloc]init];
//       [self.navigationController pushViewController:loginVc animated:YES];
    }

}



#pragma mark -
#pragma mark - 获取短信验证码
- (void)getCode
{
    
    //发送验证码短信
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.userName_tf.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
        //发送成功
        if (!error) {
            //倒计时
            [self runTime];
        }
        else{
            //发送错误
            NSLog(@"输入错误");
        }
    }];
    
}

//获取验证码倒计时
- (void)runTime
{
    __block int timeout= 60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self.getCodeBtn setTitle:@" 获取验证码 " forState:UIControlStateNormal];
                self.getCodeBtn.userInteractionEnabled = YES;
            });
        }else{
            int seconds = timeout % 61;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                //NSLog(@"____%@",strTime);
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                [self.getCodeBtn setTitle:[NSString stringWithFormat:@"%@秒后发送",strTime] forState:UIControlStateNormal];
                [UIView commitAnimations];
                self.getCodeBtn.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}


//点击空白处隐藏键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_userName_tf resignFirstResponder];
    [_passWord_tf resignFirstResponder];
    [_invite_tf resignFirstResponder];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
